package org.client.iot.ui.project.fragment;

import android.annotation.SuppressLint;
import android.util.Log;
import android.view.View;

import com.bumptech.glide.Glide;
import com.lucky.base.network.NetworkApi;
import com.lucky.base.network.observer.BaseObserver;
import com.lucky.utils.FastjsonUtils;
import com.xuexiang.xui.widget.dialog.materialdialog.MaterialDialog;

import org.client.iot.LauncherApplication;
import org.client.iot.R;
import org.client.iot.databinding.FragmentProjectMyselfBinding;
import org.client.iot.domain.UserDto;
import org.client.iot.http.ApiService;
import org.client.iot.util.MMKVUtils;
import org.tzw.template.base.BaseFragment;

import java.util.Objects;

import io.reactivex.Observable;

public class ProjectMyselfFragment extends BaseFragment<FragmentProjectMyselfBinding> {

    @Override
    protected void initData(View view) {
        getUserInfo();
    }

    @Override
    protected void initView(View view) {

    }

    @Override
    protected void initListener() {
        mRoot.btnQuit.setOnClickListener(v -> {
            new MaterialDialog.Builder(requireContext())
                    .content("确认退出吗")
                    .positiveText("确定")
                    .negativeText("取消")
                    .onPositive((dialog, which) -> {
                        MMKVUtils.getInstance().put("token", "");
                        requireActivity().finish();
                    })
                    .show();

        });
    }

    private void getUserInfo() {
        String token = "Bearer " + MMKVUtils.getInstance().get("token", "");
        Observable<UserDto> compose = NetworkApi.createService(ApiService.class,"").getInfo(token).compose(NetworkApi.applySchedulers(new BaseObserver<UserDto>() {
            @SuppressLint("CheckResult")
            @Override
            public void onSuccess(UserDto userDto) {
                if (userDto == null) {
                    return;
                }
                Log.i(TAG, "onSuccess: userDto==" + FastjsonUtils.toJsonString(userDto));

                requireActivity().runOnUiThread(() -> {
                    if (userDto.getAvatar() != null || !"null".equals(userDto.getAvatar())) {
//                        Glide.with(LauncherApplication.getContext()).load((Base64.decode(userDto.getAvatar(), Base64.DEFAULT)))
//                                .fitCenter().into(mRoot.imgMyselfIcon);
                    } else if (userDto.getAvatar() == null) {
                        Glide.with(LauncherApplication.getContext()).load(R.drawable.ic_myself).fitCenter().into(mRoot.imgMyselfIcon);
                    }
                    mRoot.textViewMyselfName.setText(userDto.getNickName() == null || "null".equals(userDto.getNickName()) ? "暂无" : userDto.getNickName());
                    mRoot.textViewMyselfWorkName.setText(userDto.getDeptName() == null || "null".equals(userDto.getDeptName()) ? "暂无" : userDto.getDeptName());
                });
            }

            @SuppressLint("CheckResult")
            @Override
            public void onFailure(Throwable e) {
                requireActivity().runOnUiThread(() -> showToastLong(e.getMessage()));
            }

        }));
    }


}